home *** CD-ROM | disk | FTP | other *** search
- /* HandyStuff.c -- Various useful functions
- */
-
- #include "HandyStuff.h"
-
-
- short
- RangedRdm (short min, short max)
- /* assume that min is less than max */
- {
- long range, t;
-
-
- range = max - min + 1;
- t = (Random()&0x7fff) % range;
-
- if (t+min > max)
- {
- SysBeep(2); SysBeep(2); SysBeep(2);
- }
- return( t+min );
- }
-
-
- // This function tries to allocate a handle from temporary memory
- // first, and only if that fails from the reserved memory.
- // This function is taken from the DarkSide of the Mac example code.
- // Note that the return value can be nil and should be checked.
- Handle
- BestNewHandle (Size s)
- {
- Handle theHandle;
- OSErr anErr;
-
- if ((theHandle = TempNewHandle(s, &anErr)) == nil)
- theHandle = NewHandle(s);
-
- return(theHandle);
- }
-
- // Whew, finally a support function I wrote all by myself!
- // This next function is like the ToolBox call PtInRect, but works directly
- // with two x/y values instead of with a Point structure.
-
- Boolean
- XYInRect (Rect *r, short beeX, short beeY)
- {
- return (beeX > r->left && beeX < r->right && beeY > r->top && beeY < r->bottom);
- }
-
-
- // Draws a string at specified coordinates. Duh.
- void
- DrawStringAt (short x, short y, Str255 str)
- {
- MoveTo(x, y);
- DrawString(str);
- }
-
-
- // Slightly more interesting: draws a string at the given
- // y-coordinate, centered horizontally in the current port.
- void
- CenterString (short y, StringPtr str)
- {
- GrafPtr port;
- short left, right, width;
-
- GetPort(&port);
- width = TextWidth(str, 1, *str);
- left = port->portRect.left;
- right = port->portRect.right;
-
- DrawStringAt(left+(((right-left)-width)/2), y, str);
- }
-
-
- // Adjusts a rectangle so that it is centered horizontally across
- // the port, with the vertical mid-line at the given y-coordinate.
- void
- CenterRectHorizontal (Rect *r, short y)
- {
- GrafPtr port;
- short left, right, width, height;
-
- GetPort(&port);
- width = r->right - r->left;
- height = r->bottom - r->top;
- left = port->portRect.left;
- right = port->portRect.right;
-
- r->left = left+(((right-left)-width)/2);
- r->right = r->left + width;
- r->top = y - height/2;
- r->bottom = r->top + height;
- }
-
-
- // Adjusts a rectangle so that it is centered vertically across
- // the port, with the horizontal mid-line at the given x-coordinate.
- void
- CenterRectVertical (Rect *r, short x)
- {
- GrafPtr port;
- short top, bottom, width, height;
-
- GetPort(&port);
-
- width = r->right - r->left;
- height = r->bottom - r->top;
- top = port->portRect.top;
- bottom = port->portRect.bottom;
-
- r->top = top+(((bottom-top)-height)/2);
- r->bottom = r->top + height;
- r->left = x - width/2;
- r->right = r->left + width;
- }
-
-
- // Adjusts a rectangle so that it is centered horizontally and vertically
- // around the given x and y coordinates.
- void
- CenterRect (Rect *r, short x, short y)
- {
- GrafPtr port;
- short top, bottom, left, right, width, height;
-
- GetPort(&port);
- width = r->right - r->left;
- height = r->bottom - r->top;
- left = port->portRect.left;
- right = port->portRect.right;
- top = port->portRect.top;
- bottom = port->portRect.bottom;
-
- r->left = x - width/2;
- r->right = r->left + width;
- r->top = y - height/2;
- r->bottom = r->top + height;
- }
-
-
- // I am a very, very, lazy person.
- void
- LineFromTo (short xFrom, short yFrom, short xTo, short yTo)
- {
- MoveTo(xFrom, yFrom);
- LineTo(xTo, yTo);
- }
-
-
- // Returns a random true/false value.
- Boolean
- RandomBool (void)
- {
- return (RangedRdm(0,1) == 1);
- }
-
-
- // This function stolen from the Glypha III source code.
- void
- RedAlert (StringPtr theStr)
- {
- #define kRedAlertID 128
- short whoCares;
-
- ParamText(theStr, "\p", "\p", "\p");
- whoCares = StopAlert(kRedAlertID, 0L);
- }
-
- // The following code was stolen from the NewsWatcher source code.
- /*----------------------------------------------------------------------------
- SetPortTextStyle
-
- Set the font, size, and style of the current port.
-
- Entry: *style = text style record.
- ----------------------------------------------------------------------------*/
-
- void
- SetPortTextStyle (TextStyle *style)
- {
- TextFont(style->tsFont);
- TextFace(style->tsFace);
- TextSize(style->tsSize);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetPortTextStyle
-
- Get the font, size, and style of the current port.
-
- Exit: *style = text style record.
- ----------------------------------------------------------------------------*/
-
- void
- GetPortTextStyle (TextStyle *style)
- {
- GrafPtr port;
-
- GetPort(&port);
-
- style->tsFont = port->txFont;
- style->tsFace = port->txFace;
- style->tsSize = port->txSize;
-
- }
-
-